<?php

/* @version $Id: config.php,v 1.16 2008-02-20 20:27:13 oleg Exp $ */


// Start session.
session_name('SID');
if (!session_id())
{
    @session_start();
}

// Start timer.
require_once 'utils.php';
header("Content-Type: text/html; charset=utf-8");

// http://php.net/manual/en/function.error-reporting.php
$ver = explode('.', PHP_VERSION);
$php_version = $ver[0];
if ($php_version == 5)
{
    error_reporting(E_ALL);
}
else
{
    error_reporting(E_ALL);
}

@set_magic_quotes_runtime(0);

define('CFG_PATH',              dirname(__FILE__));

// Folders.
define('CFG_BOOKS_DIR',         CFG_PATH.'/books/');
define('CFG_TESTS_DIR',         CFG_PATH.'/tests/');
define('CFG_LIB_DIR',           CFG_PATH.'/lib/');
define('CFG_MENU_THEMES_DIR',   CFG_PATH.'/../book_themes/');
define('CFG_ERRORS_LOG_DIR',    CFG_PATH.'/log/');
define('CFG_LOG_DIR',           CFG_PATH.'/log/');
define('CFG_ADODB_DIR',         CFG_LIB_DIR.'adodb/');
define('CFG_SMARTY_DIR',        CFG_LIB_DIR.'smarty/');
define('CFG_HELP_DIR',          'help/');
define('CFG_THEMES_DIR',        'themes/');

define('CFG_SHOW_VERSION',      TRUE);
define('CFG_CHECK_INSTALL',     TRUE);
define('CFG_WRITE_SQL_LOG',     TRUE);

define('CFG_MIN_MYSQL_VERSION', '4.1.21');

// Version information.
define('CFG_VERSION',           '3.4');
define('CFG_VERSION_DATE',      20120311);

// Test types.
define('TEST_TYPE_COMMON',      0);
define('TEST_TYPE_SCORED',      1);

// Test selection type.
define('TEST_SELECT_TEST_FROM_SECTION',  1);
define('TEST_ALL_TESTS_IN_LIST',         2);

if (!isset($no_config))
{
    if (CFG_CHECK_INSTALL)
    {
        if (file_exists("install.php") || file_exists("install/"))
        {
            echo "Remove <strong>install.php</strong> and <strong>install</strong> folder.";
            exit();
        }
    }
    define('CFG_CHECK_STATUS',  TRUE);

    if (CFG_CHECK_STATUS)
    {
        status_report();
    }
    // Load database configuration.
    require_once 'config_inc.php';

    if (!defined('CFG_HOST'))
    {
        echo 'Run install.php.';
        exit();
    }

    // URL.
    $host_len = strlen(CFG_HOST);

    if (substr(CFG_HOST, $host_len-1, 1) != '/')
    {
        define('CFG_HOST_NAME', CFG_HOST.'/');
    }
    else
    {
        define('CFG_HOST_NAME', CFG_HOST);
    }
    define('CFG_URL',       'http://'.CFG_HOST_NAME);

    // Do you want to write php error log?
    define('CFG_WRITE_ERROR_LOG',   FALSE);

    if (CFG_WRITE_ERROR_LOG)
    {
        require_once CFG_LIB_DIR.'lib.error_log.php';
    }

    // Login style.
    define('LOGIN_LIST',        1);
    define('LOGIN_DIRECT',      2);

    // Include library files.
    require_once CFG_SMARTY_DIR.'Smarty.class.php';
    require_once CFG_ADODB_DIR.'adodb.inc.php';
    require_once CFG_PATH.'/router.php';

    require_once CFG_LIB_DIR.'lib.database.php';
    require_once CFG_LIB_DIR.'lib.settings.php';
    require_once CFG_PATH.'/db.php';
    require_once CFG_LIB_DIR.'filter.php';
    require_once CFG_LIB_DIR.'is_email.php';
    require_once CFG_LIB_DIR.'form.php';
    require_once CFG_LIB_DIR.'help.php';
    require_once CFG_LIB_DIR.'query.php';
    require_once CFG_LIB_DIR.'count.php';
    require_once CFG_LIB_DIR.'xml.php';
    require_once CFG_PATH.'/views/view_base.php';
    require_once CFG_PATH.'/views/view_smarty.php';
    require_once CFG_LIB_DIR.'class.log.php';
    require_once CFG_LIB_DIR.'class.language.php';
    require_once CFG_LIB_DIR.'class.user.php';
    require_once CFG_LIB_DIR.'class.grant.php';
    require_once CFG_LIB_DIR.'class.group.php';
    require_once CFG_LIB_DIR.'class.book.php';
    require_once CFG_LIB_DIR.'class.book_course.php';
    require_once CFG_LIB_DIR.'class.chapter.php';
    require_once CFG_LIB_DIR.'class.column.php';
    require_once CFG_LIB_DIR.'class.field.php';
    require_once CFG_LIB_DIR.'class.translation.php';
    require_once CFG_LIB_DIR.'class.paginator.php';
    require_once CFG_LIB_DIR.'class.course.php';
    require_once CFG_LIB_DIR.'class.group_course.php';
    require_once CFG_LIB_DIR.'class.category.php';
    require_once CFG_LIB_DIR.'class.module.php';
    require_once CFG_LIB_DIR.'class.category_module.php';
    require_once CFG_LIB_DIR.'class.access.php';
    require_once CFG_LIB_DIR.'class.xml_item.php';
    require_once CFG_LIB_DIR.'class.query.php';
    require_once CFG_LIB_DIR.'class.module_action.php';

    // Tests.
    require_once CFG_LIB_DIR.'class.section.php';
    require_once CFG_LIB_DIR.'class.group_section.php';
    require_once CFG_LIB_DIR.'class.test.php';
    require_once CFG_LIB_DIR.'class.section.php';
    require_once CFG_LIB_DIR.'class.hint.php';
    require_once CFG_LIB_DIR.'class.theme.php';
    require_once CFG_LIB_DIR.'class.resume.php';
    require_once CFG_LIB_DIR.'class.question.php';
    require_once CFG_LIB_DIR.'class.answer.php';
    require_once CFG_LIB_DIR.'class.conclusion.php';
    require_once CFG_LIB_DIR.'class.section_test.php';
    require_once CFG_LIB_DIR.'class.user_result.php';
    require_once CFG_LIB_DIR.'class.user_answer.php';
    require_once CFG_LIB_DIR.'class.user_result_theme.php';
    require_once CFG_LIB_DIR.'class.user_result_time.php';

    // Open database connection.
    $adodb = open_database();
    $version = get_mysql_version();
    status_report($version);

    // Load settings.
    $WEB_APP['settings'] = settings_get();

    // Set timezone.
    date_default_timezone_set($WEB_APP['settings']['timezone']);

    // Set main directories.
    define('CFG_TEMPLATES_DIR',
        CFG_THEMES_DIR.$WEB_APP['settings']['theme'].'/templates/');
    define('CFG_COMPILE_DIR',
        CFG_THEMES_DIR.$WEB_APP['settings']['theme'].'/templates_c/');
    define('CFG_CSS_DIR',
        CFG_URL.CFG_THEMES_DIR.$WEB_APP['settings']['theme'].'/css/');
    define('CFG_IMAGES_DIR',
        CFG_URL.CFG_THEMES_DIR.$WEB_APP['settings']['theme'].'/images/');

    $WEB_APP['editform'] = TRUE;
    $WEB_APP['errorstext'] = '';

    $parse_url_array = parse_url(CFG_URL);
    $WEB_APP['host'] = $parse_url_array['host'];

    if (CFG_SHOW_VERSION)
    {
        $WEB_APP['version'] = CFG_VERSION;
        $WEB_APP['version_date'] = CFG_VERSION_DATE;
    }
    $WEB_APP['cfg_url'] = CFG_URL;
    $WEB_APP['images_dir'] = CFG_IMAGES_DIR;
    $WEB_APP['css_dir'] = CFG_CSS_DIR;
    $WEB_APP['help'] = $help;

    load_translations();


    // Load actions.
    require_once CFG_LIB_DIR.'class.action.php';
    require_once CFG_LIB_DIR.'class.list_action.php';

    $WEB_APP['list_actions'] = array($WEB_APP['list_action_delete']);
}

